//引入axios模块
import axios from 'axios'
import { getUserTempId } from '@/utils/getUserTempId'

//引入nprogress模块，并引入对应样式
import 'nprogress/nprogress.css'
import Nprogress from 'nprogress'

//创建axios实例对象，以后整个项目都用这个发请求
const ajax = axios.create({
    baseURL:'http://39.98.123.211/',
    timeout:5000, //设置超时时间
})

//设置请求拦截器
ajax.interceptors.request.use(
    config=> {
        Nprogress.start() //启用进度条
        console.log('请求成功拦截config',config);
        config.headers.userTempId = getUserTempId() //在请求拦截器中给请求头添加userTempId
        return config
    },
    err=>{
        console.log('请求失败拦截',err);
        return Promise.reject(new Error(err))
    }
)

//设置响应拦截器
ajax.interceptors.response.use(
    response => {
        Nprogress.done() //结束进度条
        console.log("响应成功拦截response",response);
        console.log('响应成功拦截response.data',response.data);
        return response.data
    },
    err => {
        Nprogress.done() //结束进度条
        console.log('响应失败拦截err',err);
        return Promise.reject(new Error(err))
    }
)

//导出对象
export default ajax
